package utilities.nonessential;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class InstructionGenerator {

	BufferedReader in;
	BufferedWriter out;

	InstructionGenerator(File input, File output) throws IOException {
		this.in = new BufferedReader(new FileReader(input));
		this.out = new BufferedWriter(new FileWriter(output));
	}

	public void generateInstruction(int numInst) throws IOException {
		for (int i = 0; i < numInst; i++) {
			this.out.write(this.parseInstruction() + "\n");
		}
		this.out.close();
	}

	private String parseInstruction() throws IOException {
		String opBefore;
		String nopBefore;
		String rBefore;
		String xBefore;
		int xBeforeInt;
		String sBefore;
		opBefore = this.parseLine();
		nopBefore = this.parseLine();
		rBefore = this.parseLine();
		xBefore = this.parseLine();
		xBeforeInt = Integer.parseInt(xBefore) * 4;
		sBefore = this.parseLine();
		String opAfter = this.decodeOp(opBefore);
		String rAfter = rBefore;
		String xAfter = Integer.toHexString(xBeforeInt).toUpperCase();
		String sAfter = sBefore.toUpperCase();
		if (nopBefore == "1" || nopBefore == "T" || nopBefore == "t"
				|| nopBefore == "Y" || nopBefore == "y") {
			xAfter = "FF";
			rAfter = "FF";
		}
		return opAfter + rAfter + xAfter + sAfter;
	}

	private String decodeOp(String opBefore) {
		String op = opBefore;
		String result = "X";
		if (op.equals("ld") || op.equals("LD") || op.equals("0")) {
			result = "0";
		}
		if (op.equals("ldi") || op.equals("LDI") || op.equals("1")) {
			result = "1";
		}
		if (op.equals("st") || op.equals("ST") || op.equals("2")) {
			result = "2";
		}
		if (op.equals("add") || op.equals("ADD") || op.equals("3")) {
			result = "3";
		}
		if (op.equals("sub") || op.equals("SUB") || op.equals("4")) {
			result = "4";
		}
		if (op.equals("mul") || op.equals("MUL") || op.equals("5")) {
			result = "5";
		}
		if (op.equals("div") || op.equals("DIV") || op.equals("6")) {
			result = "6";
		}
		if (op.equals("or") || op.equals("OR") || op.equals("7")) {
			result = "7";
		}
		if (op.equals("and") || op.equals("AND") || op.equals("8")) {
			result = "8";
		}
		if (op.equals("shl") || op.equals("SHL") || op.equals("9")) {
			result = "9";
		}
		if (op.equals("shr") || op.equals("SHR") || op.equals("A")
				|| op.equals("a")) {
			result = "A";
		}
		if (op.equals("io") || op.equals("IO") || op.equals("B")
				|| op.equals("b")) {
			result = "B";
		}
		if (op.equals("br") || op.equals("BR") || op.equals("C")
				|| op.equals("c")) {
			result = "C";
		}
		if (op.equals("brz") || op.equals("BRZ") || op.equals("D")
				|| op.equals("d")) {
			result = "D";
		}
		if (op.equals("brn") || op.equals("BRN") || op.equals("E")
				|| op.equals("e")) {
			result = "E";
		}
		if (op.equals("brs") || op.equals("BRS") || op.equals("F")
				|| op.equals("f")) {
			result = "F";
		}

		return result;
	}

	private java.lang.String parseLine() throws IOException {
		String line = this.in.readLine();
		return line.substring(4);
	}
}
